package org.chainsale.common;

import java.util.concurrent.atomic.AtomicInteger;

public class IdGenerator {
	private static final AtomicInteger indexer = new AtomicInteger(1);

	private static final ThreadLocal<Integer> idLocal = new ThreadLocal<Integer>(){
		protected Integer initialValue() {
			return indexer.get();
		};
		public Integer get() {
			return indexer.getAndAdd(1);
		};
	};
	
	public static int getNextId() {
		return idLocal.get();
	}
	
	public static void main(String[] args) {
		
		new ThreadA(1).start();
		new ThreadA(2).start();
		new ThreadA(3).start();
		new ThreadA(4).start();
		
	}
	
	public static class ThreadA extends Thread {
		
		private int tId;
		
		public ThreadA(int tId) {
			this.tId = tId;
		}
		
		@Override
		public void run() {
			System.out.println("Thread[" + tId + "] Next Id is " + getNextId() + ".");
			
		}
	}
	
}
